#include <iostream>
#include "HttpServer.hpp"
#include <memory>
#include "Usage.hpp"
#include "Util.hpp"
#include<fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

//http的web根目录
#define ROOT "./wwwroot"
//如果客户端只请求一个/，我们返回默认首页 ./wwwroot/index.html
#define HOMEPAGE "index.html"

void HandlerHttpRequest(int sockfd)
{
    // 1.读取请求
    char buffer[10240];
    ssize_t s = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
    if (s > 0)
    {
        buffer[s] = 0;
        std::cout << buffer << "----------------------------\n"
                  << std::endl;
    }
    std::vector<std::string> vline;
    Util::cutString(buffer, "\n", &vline);
    for(auto & e:vline)
    {  
        std::cout<<e<<std::endl;
    }
    std::vector<std::string> vblock;
    Util::cutString(vline[0], " ", &vblock);
    std::string file = vblock[1]; // 获取http请求的资源，是一个路径
    std::string target=ROOT;
    if(file=="/")file="/index.html";
    target+=file;
    std::cout << target << std::endl;

    std::string content;
    std::ifstream in(target);//C++文件流
    if(in.is_open())
    {
        std::string line;
        while(std::getline(in,line))
        {
            content+=line;
        }
        in.close();
    }

    std::string HttpResponse;
    if(content.empty())HttpResponse="HTTP/1.1 404 NotFound\r\n";
    else HttpResponse="HTTP/1.1 200 OK\r\n";
    HttpResponse+="\r\n";
    HttpResponse+=content;
    send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
    // 2.尝试构建一个http的响应
    // std::string HttpResponse;
    // HttpResponse = "HTTP/1.1 200 OK\r\n";
    // HttpResponse += "\r\n";
    // HttpResponse += "<html><h3>hello 106 students!</h3></html>";
    // send(sockfd, HttpResponse.c_str(), HttpResponse.size(), 0);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    std::unique_ptr<HttpServer> httpserver(new HttpServer(atoi(argv[1]), HandlerHttpRequest));
    httpserver->Start();
    return 0;
}